package pl.shenlon.poker.gui;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import net.miginfocom.swing.MigLayout;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import pl.shenlon.ga.BehaviourPattern;
import pl.shenlon.ga.Specimen;
import pl.shenlon.poker.game.GameState;

public class SpecimenDataView extends JPanel {
	public static final String BET_PROFITABLE_TRUE_TEXT = "TAK",
			BET_PROFITABLE_FALSE_TEXT = "NIE";

	/**
	 * 
	 */
	private static final long serialVersionUID = -5882832847206574248L;

	private JScrollPane behaviourPatternScrollPane;

	private JTable behaviourPatternTable;
	private DefaultTableModel behaviourPatternTableModel;
	private ChartPanel chartPanel;
	private Specimen specimen;
	private String[] tableHeaders;

	public SpecimenDataView() {
		initComponent();
	}

	private void changeChart() {
		int row = behaviourPatternTable
				.convertRowIndexToModel(behaviourPatternTable.getSelectedRow());
		GameState gameState = new GameState(
				(String) behaviourPatternTable.getValueAt(row, 1),
				(String) behaviourPatternTable.getValueAt(row, 2),
				(String) behaviourPatternTable.getValueAt(row, 0),
				behaviourPatternTable.getValueAt(row, 3).equals(
						BET_PROFITABLE_TRUE_TEXT) ? true : false);
		chartPanel.setChart(prepareChart(specimen.getGenom()
				.getBehaviourPattern(gameState)));
	}

	public Specimen getSpecimen() {
		return specimen;
	}

	private void initComponent() {
		setLayout(new MigLayout());

		behaviourPatternTableModel = new DefaultTableModel(0, 4) {
			/**
			 * 
			 */
			private static final long serialVersionUID = -7341011635806855804L;

			@Override
			public boolean isCellEditable(int row, int col) {
				return false;
			}
		};
		tableHeaders = new String[]{
				"RUNDA GRY",
				"SZANSA NA WYGRANĄ",
				"SZANSA NA WZMOCNIENIE",
				"ZAKŁAD OPŁACALNY",};
		behaviourPatternTableModel.setColumnIdentifiers(tableHeaders);
		for (GameState gameState : GameState.POSSIBLE_GAME_STATES) {
			Object[] rowData = new Object[]{
					gameState.getRound(),
					gameState.getWinChanceTerm(),
					gameState.getStrengthenChanceTerm(),
					gameState.isBetProfitable()
							? BET_PROFITABLE_TRUE_TEXT
							: BET_PROFITABLE_FALSE_TEXT,};
			behaviourPatternTableModel.addRow(rowData);
		}

		behaviourPatternTable = new JTable(behaviourPatternTableModel);

		behaviourPatternTable.setAutoCreateRowSorter(true);
		behaviourPatternTable.setFillsViewportHeight(true);
		behaviourPatternTable
				.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		behaviourPatternTable.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (!e.getValueIsAdjusting()) {
							changeChart();
						}
					}
				});

		behaviourPatternScrollPane = new JScrollPane(behaviourPatternTable);

		JFreeChart demoChart = prepareChart(new BehaviourPattern(new int[]{
				0,
				0,
				0,
				0,
				0}));
		chartPanel = new ChartPanel(demoChart);

		add(behaviourPatternScrollPane, "growy, w 50%");
		add(chartPanel, "growy, w 50%");
	}

	private JFreeChart prepareChart(BehaviourPattern pattern) {
		DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
		String rowKey = "Osobnik";
		dataSet.addValue(pattern.getBluffChance(), rowKey, "BLUFF");
		dataSet.addValue(pattern.getCallChance(), rowKey, "CALL");
		dataSet.addValue(pattern.getCheckChance(), rowKey, "CHECK");
		dataSet.addValue(pattern.getFoldChance(), rowKey, "FOLD");
		dataSet.addValue(pattern.getRaiseChance(), rowKey, "RAISE");


		SpiderWebPlot plot = new SpiderWebPlot(dataSet);
		plot.setStartAngle(54D);
		plot.setInteriorGap(0.40000000000000002D);
		plot.setLabelGenerator(new StandardCategoryItemLabelGenerator() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 8882869001164238275L;

			@Override
			public String generateColumnLabel(CategoryDataset dataset, int col) {
				return dataset.getColumnKey(col) + " "
						+ dataset.getValue(0, col);
			}
		});
		JFreeChart chart = new JFreeChart(plot);
		return chart;
	}

	public void setSpecimen(Specimen specimen) {
		this.specimen = specimen;
		behaviourPatternTable.changeSelection(0, 0, false, false);
		changeChart();
	}
}
